/**
 * @file
 * @auth jinguangguo
 * @date 2016/10/17
 */

import gulp from 'gulp';
import swig from 'gulp-swig';
import replace from 'gulp-replace';
import minifyHtml from 'gulp-minify-html';

const reload = global.browserSync.reload;

const FILE_PATHS = {
    HTML_ROOT: './src/cn/*.html',
    HTML_CN: './src/cn/*.html',
    HTML_HK: './src/hk/*.html',
    HTML_US: './src/us/*.html',
    HTML_ES: './src/es/*.html',
    HTML_THIRD: './src/third/*.html'
};

gulp.task('html:dev:root', function () {
    return gulp.src(FILE_PATHS.HTML_ROOT)
        .pipe(swig())
        .pipe(replace('src="../', 'src="./'))
        .pipe(replace('href="../', 'href="./'))
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev'))
});

gulp.task('html:dev:hk', function () {
    return gulp.src('./src/hk/*.html')
        .pipe(swig())
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev/hk'))
});

gulp.task('html:dev:cn', function () {
    return gulp.src('./src/cn/*.html')
        .pipe(swig())
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev/cn'))
});

gulp.task('html:dev:us', function () {
    return gulp.src('./src/us/*.html')
        .pipe(swig())
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev/us'))
});

gulp.task('html:dev:es', function () {
    return gulp.src('./src/es/*.html')
        .pipe(swig())
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev/es'))
});

gulp.task('html:dev:third', function () {
    return gulp.src('./src/third/*.html')
        .pipe(swig())
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev/third'))
});

gulp.task('html:dev',['html:dev:root', 'html:dev:us', 'html:dev:es', 'html:dev:hk', 'html:dev:cn', 'html:dev:third'], function (cb) {
    cb();
});

function todo(path) {
    global.logger.info(path + ' start ........');

    let lang = path.split('/')[2];

    return gulp.src(path)
        .pipe(swig({defaults: { cache: false }}))
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev/' + lang))
        .pipe(reload({stream: true}))
}

function todoRoot(path) {
    global.logger.info('[root]' + path + ' start ........');

    return gulp.src(path)
        .pipe(swig({defaults: { cache: false }}))
        .pipe(replace('src="../', 'src="./'))
        .pipe(replace('href="../', 'href="./'))
        .pipe(replace('scss', 'css'))
        .pipe(gulp.dest('./dev'))
        .pipe(reload({stream: true}))
}

// const HTML_ROOT = './src/us/*.html';
// const HTML_HK = './src/hk/*.html';
// const HTML_ES = './src/es/*.html';
// const HTML_THIRD = './src/third/*.html';

gulp.task('html:server:root', function () {
    const files = FILE_PATHS.HTML_ROOT;

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        return todoRoot(path);
    });

    return todoRoot(files);

});

gulp.task('html:server:cn', function () {

    const files = FILE_PATHS.HTML_CN;

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        return todo(path);
    });

    return todo(files);

});

gulp.task('html:server:hk', function () {

    const files = FILE_PATHS.HTML_HK;

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        return todo(path);
    });

    return todo(files);

});

gulp.task('html:server:us', function () {

    const files = FILE_PATHS.HTML_US;

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        return todo(path);
    });

    return todo(files);

});

gulp.task('html:server:es', function () {

    const files = FILE_PATHS.HTML_ES;

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        return todo(path);
    });

    return todo(files);

});

gulp.task('html:server:third', function () {

    const files = FILE_PATHS.HTML_THIRD;

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        return todo(path);
    });

    return todo(files);

});

gulp.task('html:server', ['html:server:root', 'html:server:cn', 'html:server:us', 'html:server:es', 'html:server:hk', 'html:server:third'], function (cb) {

    const files = './src/include/**/*.html';

    gulp.watch([
        files
    ], function (event) {
        let path = event.path;
        global.logger.info(path + ' changed !!!');
        let key, obj, temp;
        for (key in FILE_PATHS) {
            temp = FILE_PATHS[key];
            if (key === 'HTML_ROOT') {
                global.logger.info(111111);
                todoRoot(temp);
            } else {
                global.logger.info(222222);
                todo(temp);
            }
        }
    });

    cb();

});

gulp.task('html:prod', ['html:dev'], function () {
    return gulp.src('./dev/**/*.html')
        .pipe(minifyHtml({
            empty: true, // 保留空属性
            cdata: false, // 保留scripts标签的CDATA
            comments: false, // 保留注释
            conditionals: true, // 保留条件语句
            spare: true, // 保留多余属性
            quotes: true, // 保留所有引用
            loose: false // 保留1个空白符
        }))
        .pipe(gulp.dest('./prod'));
});
